<jsp:useBean id="rr" class="bibbletex.models.RequestReference"
	scope="page" />
<jsp:useBean id="sessiondata" class="bibbletex.models.SessionData"
	scope="session" />
<jsp:setProperty name="rr" property="*" />
<%@ page import="java.util.ArrayList,bibbletex.models.Collaboration, bibbletex.models.Reference"
         import="bibbletex.models.SessionData,bibbletex.helpers.BibtexFormat, java.io.*" 
         import="bibbletex.helpers.FileHelper, java.util.List"%>

<%
	Collaboration c = sessiondata.collaboration;
	Reference r;
	boolean forwardToReferenceShow = true;
	
	if (c != null){
		if (request.getHeader("referer").contains("import.jsp")){
			List<Reference> refs = new ArrayList<Reference>();
			if (rr.getBibtexcode() != null)
				refs = BibtexFormat.toReference(rr.getBibtexcode().replaceAll("\\\\n ", ""));
			else {
				String contentType = request.getContentType();
			    if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
					String str = FileHelper.readFile(new DataInputStream(request.getInputStream()), request.getContentLength(), request.getContentType());
					refs.addAll(BibtexFormat.toReference(str.replaceAll("\\\\n", "")));				
				}
			}
			for (Reference ref : refs)
			{
				ref.setIs_suggestion(!sessiondata.collaboration_is_editable);
				c.addReference(ref);
			}
			forwardToReferenceShow = false;
		}
		else
		{
			if (request.getHeader("referer").contains("edit.jsp"))
				r = new Reference(rr, sessiondata.reference.getId());
			else
				r = new Reference(rr);
			r.setIs_suggestion(!sessiondata.collaboration_is_editable);
			c.addReference(r);
			sessiondata.reference = r;
		}
	}
%>

<% if (c != null) { %>
	<% if (forwardToReferenceShow) { %>
	<jsp:forward page="/collaboration/reference/show.jsp" />
	<% } else { %>
	<jsp:forward page="/collaboration/show.jsp" />
	<% } %>
<% } else { %>

<%
	String page_title = new String("Process reference");
	boolean show_title = true;
	String onloadjsfunction = "";
%>
<%@ include file="/header.jsp"%>

<p>ERROR: The collaboration could not be loaded</p>

<%@ include file="/footer.jsp"%>

<% } %>
